package com.meituan.catering.management.order.biz.service.impl;

import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.common.model.biz.UserContextBO;
import com.meituan.catering.management.order.biz.model.CateringOrderDetailBO;
import com.meituan.catering.management.order.biz.model.converter.*;
import com.meituan.catering.management.order.biz.model.request.SearchCateringOrderBizRequest;
import com.meituan.catering.management.order.biz.service.CateringOrderQueryService;
import com.meituan.catering.management.order.dao.converter.SearchCateringOrderConverter;
import com.meituan.catering.management.order.dao.mapper.CateringOrderItemAccessoryMapper;
import com.meituan.catering.management.order.dao.mapper.CateringOrderItemMapper;
import com.meituan.catering.management.order.dao.mapper.CateringOrderMapper;
import com.meituan.catering.management.order.dao.model.CateringOrderDO;
import com.meituan.catering.management.order.dao.model.CateringOrderItemAccessoryDO;
import com.meituan.catering.management.order.dao.model.CateringOrderItemDO;
import com.meituan.catering.management.order.dao.model.request.SearchCateringOrderDataRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Believe
 */
@Service
public class CateringOrderQueryServiceImpl implements CateringOrderQueryService {

    @Resource
    private CateringOrderMapper cateringOrderMapper;

    @Resource
    private CateringOrderItemMapper cateringOrderItemMapper;

    @Resource
    private CateringOrderItemAccessoryMapper cateringOrderItemAccessoryMapper;

    @Override
    public PageBO searchCateringOrder(UserContextBO userContextBO, SearchCateringOrderBizRequest searchCateringOrderBizRequest) {
        SearchCateringOrderDataRequest searchCateringOrderData = SearchCateringOrderConverter.toSearchCateringOrderDataRequest(searchCateringOrderBizRequest);
        int countAll = cateringOrderMapper.countAll(searchCateringOrderData);
        if (countAll == 0) {
            return null;
        }
        List<CateringOrderDO> cateringOrderDOList = cateringOrderMapper.searchForPage(searchCateringOrderData);
        return PageBOConverter.toPageBO(cateringOrderDOList, countAll, searchCateringOrderData);
    }

    @Override
    public CateringOrderDetailBO findById(UserContextBO userContextBO, Long orderId) {
        CateringOrderDO cateringOrderDO = cateringOrderMapper.findById(userContextBO.getTenantId(), userContextBO.getUserId(), orderId);
        List<CateringOrderItemDO> orderItemDOList = cateringOrderItemMapper.findById(userContextBO.getTenantId(), orderId);
        Set<Long> orderItemIds = new HashSet<>();
        for (CateringOrderItemDO cateringOrderItemBO : orderItemDOList) {
            Long orderItemId = cateringOrderItemBO.getId();
            orderItemIds.add(orderItemId);
        }
        List<CateringOrderItemAccessoryDO> accessoryDOList = cateringOrderItemAccessoryMapper.findById(userContextBO.getTenantId(), orderItemIds);
        return CateringOrderDetailBOConverter.toCateringOrderDetailBO(
                CateringOrderBOConverter.toCateringOrderBO(cateringOrderDO),
                CateringOrderItemBOConverter.toCateringOrderItemBO(orderItemDOList),
                CateringOrderItemAccessoryBOConverter.toCateringOrderItemAccessoryBO(accessoryDOList)
        );
    }
}
